package com.demo;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

public class DistuptorStart {

	public static void main(String[] args) {
		// 事件工厂
		EventFactory<LongEvent> eventFactory = new LongEventFactory();

		ExecutorService executor = Executors.newSingleThreadExecutor();
		int ringBufferSize = 1024 * 1024; // RingBuffer 大小，必须是 2 的 N 次方；

		Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(eventFactory, ringBufferSize, executor,
				ProducerType.SINGLE, new YieldingWaitStrategy());

		EventHandler<LongEvent> eventHandler = new LongEventHandler();
		disruptor.handleEventsWith(eventHandler);
		disruptor.start();
		System.out.println("disruptor 启动成功!!!");

		// 发布事件
		RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();
		long sequence = ringBuffer.next();// 请求下一个事件序号；
		try {
			LongEvent event = ringBuffer.get(sequence);// 获取该序号对应的事件对象；
			long data = System.currentTimeMillis();
			event.setValue(data);
		} finally {
			ringBuffer.publish(sequence);// 发布事件；
		}
	}
}
